package com.alex.springboot02web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RequestAttributeTestController {

    @GetMapping("/test/RequestAttribute")
    public String test01(HttpServletRequest request, Model model) {
        request.setAttribute("message", "红豆生南国");
        request.setAttribute("code", 200);
        return "forward:/test/success";
    }

    @ResponseBody
    @GetMapping("/test/success")
    public Object success(@RequestAttribute("message") String message,
                          @RequestAttribute("code") Integer code,
                          HttpServletRequest request) {
        HashMap<String, Object> content = new HashMap<>();
        content.put("message_@RequestAttribute", message);
        content.put("message_request.getAttribute", request.getAttribute("message"));
        return content;
    }
}
